package com.example.hidesms.dao;

import android.database.Cursor;
import android.util.Log;

import com.example.hidesms.dto.BlockSmsItem;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.sqlite.DataBaseHelper;

public class BlockSmsDAO implements IBlockSmsDAO{
	public static String TABLE_NAME = "BLOCK_SMS";
	private static BlockSmsDAO instance;

	public static final String CREATE_TABLE_BLOCK_SMS = "" + "CREATE TABLE BLOCK_SMS ("
			+ "SmsID     integer PRIMARY KEY AUTOINCREMENT,"
			+ "Name       nvarchar(20)," 
			+ "Number       nvarchar(15)," 
			+ "Image     varbinary(100)" +");";

	public static BlockSmsDAO getInstance() {
		if (instance == null) {
			instance = new BlockSmsDAO();
		}

		return instance;
	}

	@Override
	public boolean insert(DataBaseHelper myDatabase, BlockSmsItem sms) {
		String sql = "INSERT INTO "
				+ TABLE_NAME
				+ "(Name, Number, Image)"
				+ " VALUES(?, ?, ?)";
		try {
			myDatabase.ExeSQLData(
					sql,
					new Object[] { sms.getName(), sms.getNumber(), sms.getImage()});
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	public boolean update(DataBaseHelper myDatabase, BlockSmsItem sms) {
		String sql = "UPDATE " + TABLE_NAME + " SET " + "Name = ?," + "Number = ?,"
				+ "Image = ?"
				+ " WHERE SmsID = ?";
		try {
			myDatabase.ExeSQLData(
					sql,
					new Object[] { sms.getName(), sms.getNumber(), sms.getImage()});
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	public boolean delete(DataBaseHelper myDatabase, int smsId) {
		String sql = "DELETE FROM " + TABLE_NAME + " WHERE SmsID = " + smsId;
		try {
			myDatabase.ExeSQLData(sql);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	public Cursor getAll(DataBaseHelper myDatabase, String folderGroup, boolean flag) {
		String sql = "SELECT * FROM " + TABLE_NAME+" n ";// + " n WHERE n.FolderName =" + folderGroup;
		sql+=" ORDER BY Name ASC";
		Cursor cursor = null;
		try {
			cursor = myDatabase.QueryData(sql);
		} catch (Exception e) {
			e.printStackTrace();

		}
		Log.d("test", "" + cursor == null ? "null" : "not null");
		return cursor;
	}

	@Override
	public Cursor getById(DataBaseHelper myDatabase, int smsId) {
		String sql = "SELECT * FROM " + TABLE_NAME + " n WHERE n.SmsID =" + smsId;
		Cursor cursor = null;
		try {
			cursor = myDatabase.QueryData(sql);
		} catch (Exception e) {
			e.printStackTrace();

		}
		Log.d("test", "" + cursor == null ? "null" : "not null");
		return cursor;
	}

	@Override
	public Cursor getAllByRead(DataBaseHelper myDatabase, String read) {
		String sql = "SELECT * FROM " + TABLE_NAME + " n WHERE n.ReadState =" +"'"+read+"'";
//		if ("1".equals(read)) {
//			sql += " n WHERE n.ReadState ="+read;
//		}
		Cursor cursor = null;
		try {
			cursor = myDatabase.QueryData(sql);
		} catch (Exception e) {
			e.printStackTrace();

		}
		Log.d("test", "" + cursor == null ? "null" : "not null");
		return cursor;
	}

	@Override
	public Cursor getByAdress(DataBaseHelper myDatabase, String address) {
		String sql = "SELECT * FROM " + TABLE_NAME + " n WHERE n.Number LIKE" +"'"+address+"'"+" ORDER BY TIME DESC";
		Cursor cursor = null;
		try {
			cursor = myDatabase.QueryData(sql);
		} catch (Exception e) {
			e.printStackTrace();

		}
		Log.d("test", "" + cursor == null ? "null" : "not null");
		return cursor;
	}

}

